package de.tu_darmstadt.gdi1.xcom.tests.testcases.students;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel2;

/**
 * 
 * Tests if ammo is in/decreased correctly
 * 
 * @author Lukas Schwoebel
 *
 */
public class AmmoTest {
	
	private XcomTestAdapterLevel2 testAdapter;
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		testAdapter = new XcomTestAdapterLevel2();
	}

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel2#ammoLeft()}.
	 */
	@Test
	public final void testAmmoAfterLoading() {
		
		testAdapter.loadLevelFromString(TestLevel.testLevel);
		
		// per default all players should have 10 ammo
		testAdapter.changeActivePlayer(4, 2);
		assertEquals("All players should have 10 ammo at the beginning in standardlevel", 10, testAdapter.ammoLeft());
		testAdapter.changeActivePlayer(2, 5);
		assertEquals("All players should have 10 ammo at the beginning in standardlevel", 10, testAdapter.ammoLeft());
		testAdapter.changeActivePlayer(4, 6);
		assertEquals("All players should have 10 ammo at the beginning in standardlevel", 10, testAdapter.ammoLeft());
		
		// now test the savegame
		testAdapter.loadLevelFromString(TestLevel.saveGame1);
		testAdapter.changeActivePlayer(2, 1);
		assertEquals("In standardsavegame the player at 2:1 should have 10 ammo", 10, testAdapter.ammoLeft());
		testAdapter.changeActivePlayer(2, 5);
		assertEquals("in standardsavegame the player at 2:5 should have 11 ammo", 11, testAdapter.ammoLeft());
		testAdapter.changeActivePlayer(4, 6);
		assertEquals("in standardsavegame the player at 4:6 should have 3 ammo", 3, testAdapter.ammoLeft());
	}
	
	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel2#ammoLeft()}.
	 */
	@Test
	public final void testAmmoDecremented() {
		
		testAdapter.loadLevelFromString(TestLevel.saveGame1);
		
		testAdapter.changeActivePlayer(4, 6);
		testAdapter.shoot();
		assertEquals("After shooting, player at 4:6 should have only 2 ammo left:\n" + testAdapter.returnGamestateAsSavegame(),
				2, testAdapter.ammoLeft());
		testAdapter.shoot();
		assertEquals("After shooting twice, player at 4:6 should have only 1 ammo left:\n" + testAdapter.returnGamestateAsSavegame(),
				1, testAdapter.ammoLeft());
		testAdapter.shoot();
		assertEquals("After shooting three times, player at 4:6 should have 0 ammo left:\n" + testAdapter.returnGamestateAsSavegame(),
				0, testAdapter.ammoLeft());
		testAdapter.shoot();
		assertEquals("After shooting four times, player at 4:6 should still have 0 ammo left:\n" + testAdapter.returnGamestateAsSavegame(),
				0, testAdapter.ammoLeft());
		
	}

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel2#ammoLeft()}.
	 */
	@Test
	public final void testPickUpAmmo() {
		
		// insert ammo-box
		String addingAmmoToTestlevel = TestLevel.saveGame1.replaceFirst("##\\.\\.\\.#######", "##..m#######");
		// load savegame
		testAdapter.loadLevelFromString(addingAmmoToTestlevel);
		
		testAdapter.changeActivePlayer(2, 1);
		assertEquals("After loading the standardsavegame, player at 2:1 should have 10 ammo left", 10, testAdapter.ammoLeft());
		
		
		assertEquals("On 4:1 there should be an ammo-box:\n" + testAdapter.returnGamestateAsSavegame(), 'm', testAdapter.elementOnPosition(4, 1));
		
		// go and pick up ammo-box
		testAdapter.rotateActivePlayer(true);
		testAdapter.performActionForActivePlayer(); // walk
		testAdapter.performActionForActivePlayer(); // pick up ammo
		
		// now the ammo-box should be away
		assertEquals("After picking up, the ammo-box should disappear on 4:1" + testAdapter.returnGamestateAsSavegame(), '.', testAdapter.elementOnPosition(4, 1));
		assertEquals("After picking up, the player at (now) 3:1 should have 15 ammo", 15, testAdapter.ammoLeft());
		
	}
}
